package cn.lili.controller.user;

import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.distribution.entity.vos.HomeResult;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.store.entity.dto.StoreApplyDTO;
import cn.lili.modules.store.entity.dto.StoreEditDTO;
import cn.lili.modules.store.entity.dto.StoreOwnerEditDTO;
import cn.lili.modules.store.entity.dto.StoreSearchParams;
import cn.lili.modules.store.entity.vos.StoreLicenceVO;
import cn.lili.modules.store.entity.vos.StoreManagementCategoryVO;
import cn.lili.modules.store.entity.vos.StoreVO;
import cn.lili.modules.store.service.StoreService;
import cn.lili.modules.urbansubstation.entity.vos.UrbanSubstationVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 店铺管理接口
 *
 * @author Bulbasaur
 * @since 2020/12/6 16:09
 */
@Api(tags = "店铺管理接口")
@RestController
@RequestMapping("/user/store")
@RequiredArgsConstructor
@Validated
public class StoreController {

    /**
     * 店铺
     */
    private final StoreService storeService;

    private final UserService userService;


    @ApiOperation(value = "添加店铺")
    @PostMapping(value = "/add")
    public ResultMessage<Store> add(@Valid @RequestBody StoreApplyDTO storeApplyDTO) {
        if(storeApplyDTO.getBusinessCategory() == null || storeApplyDTO.getBusinessCategory().length == 0) {
            throw new ServiceException("店铺经营类目未填写");
        }
        return ResultUtil.data(storeService.add(storeApplyDTO));
    }

    @ApiOperation(value = "编辑店铺")
    @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "path", dataType = "String")
    @PutMapping(value = "/{id}")
    public ResultMessage<Store> edit(@Valid @PathVariable String id, StoreEditDTO storeEditDTO) {
        storeEditDTO.setStoreId(id);
        return ResultUtil.data(storeService.edit(storeEditDTO));
    }


    @ApiOperation(value = "获取自己店铺信息")
    @GetMapping(value = "/owner")
    public ResultMessage<StoreVO> get() {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        StoreVO storeDetail = storeService.getStoreDetailVO(currentUser.getExtendId());
        storeDetail.setNickName(currentUser.getNickName());
        User user = userService.getById(currentUser.getId());
        if (user!=null) {
            storeDetail.setOpenId(user.getOpenId());
            storeDetail.setAccount(user.getAccount());
            storeDetail.setFyName(user.getFyName());
        }
        //获取当前登录商家内容
        return ResultUtil.data(storeDetail);
    }

    @ApiOperation(value = "店铺自行编辑店铺")
    @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "path", dataType = "String")
    @PutMapping(value = "/owner/edit")
    public ResultMessage<Store> ownerEdit(@Valid @RequestBody StoreOwnerEditDTO storeEditDTO) {
        storeService.ownerEdit(storeEditDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取店铺分页列表")
    @GetMapping
    public ResultMessage<Page<StoreVO>> getByPage(StoreSearchParams searchParams, PageVO page) {
        return ResultUtil.data(storeService.findByConditionPage(searchParams, page));
    }

    @ApiOperation(value = "获取店铺详情")
    @ApiImplicitParam(name = "storeId", value = "店铺ID", required = true, paramType = "path", dataType = "String")
    @GetMapping(value = "/{storeId}")
    public ResultMessage<StoreVO> detail(@PathVariable String storeId) {
        return ResultUtil.data(storeService.getStoreDetailVO(storeId));
    }


    @ApiOperation(value = "审核店铺申请")
    @ApiImplicitParams({@ApiImplicitParam(name = "passed", value = "是否通过审核 true 通过 false 拒绝 编辑操作则不需传递", dataType = "boolean"),
            @ApiImplicitParam(name = "id", value = "店铺id", required = true, paramType = "path", dataType = "String")})
    @PutMapping(value = "/audit/{id}/{passed}")
    public ResultMessage<Object> audit(@PathVariable String id, @PathVariable Boolean passed, @RequestParam(required = false) String reason) {
        storeService.audit(id, passed, reason);
        return ResultUtil.success();
    }


    @DemoSite
    @ApiOperation(value = "关闭店铺")
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/disable/{id}")
    public ResultMessage<Store> disable(@PathVariable String id) {
        storeService.disable(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "开启店铺")
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/enable/{id}")
    public ResultMessage<Store> enable(@PathVariable String id) {
        storeService.enable(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "查询一级分类列表")
    @ApiImplicitParam(name = "storeId", value = "店铺id", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/managementCategory/{storeId}")
    public ResultMessage<List<StoreManagementCategoryVO>> firstCategory(@PathVariable String storeId) {
        return ResultUtil.data(this.storeService.goodsManagementCategory(storeId));
    }


    @ApiOperation(value = "通过id获取店铺详细信息-营业执照")
    @ApiImplicitParam(name = "id", value = "店铺ID", required = true, paramType = "path")
    @GetMapping(value = "/{id}/licence")
    public ResultMessage<StoreLicenceVO> licencePhoto(@NotNull @PathVariable String id) {
        return ResultUtil.data(storeService.getStoreLicenceVO(id));
    }


    /**
     * 获取城市站点与店铺集合
     */
    @GetMapping("/getUrbanAndStoreList")
    public ResultMessage<List<UrbanSubstationVo>> getUrbanAndStoreList() {
        return storeService.getUrbanAndStoreList();
    }

    /**
     * 统计供应商新增多少家
     */
    @GetMapping("/counting/supplier/additions")
    public ResultMessage<HomeResult> countingSupplierAdditions(){
        return storeService.countingSupplierAdditions();
    }


}
